import { queryCutInList, queryCutInLib, queryMatType, queryicMatTime, queryTempType, updateMatUrl, addMatExample, queryImeDetail,addCutInTask} from '../services/api';
import { message } from 'antd';
import moment from 'moment';

function method(str, a, b) {
    let type = 'string';
    if (str === 'updateTime') {
        type = 'date';
    } else if (str === 'progId') {
        type = 'num';
    }
    switch (type) {
        case 'num': // 数字排序
            return a - b;
        case 'date': // 日期排序，IE8下'2012-12-12'这种格式无法设置时间，替换成'/'
            return moment(a).unix() - moment(b).unix();
        default:
            // 字符串排序
            return a.localeCompare(b);
    }
}
export default {
    namespace: 'cutinlib',

    state: {
        list: [],
        matTypeCodes: [],
        matTimeCodes: [],
        tempType: [],
        imeDetail: {},
        matExampleUrl: "",
        pagination: {
            current: 1,
            pageSize: 10,
        },

    },

    effects: {
        *fetch({ payload }, { call, put }) {
            const response = yield call(queryCutInLib, payload);
            let { icMatExamples } = response;
            let pageSize = 10;
            if (payload.pageSize) {
                pageSize = payload.pageSize * 1;
            }

            const result = {
                list: Array.isArray(icMatExamples) ? icMatExamples : [],
                pagination: {
                    total: response.pageInfo.pageTotal,
                    pageSize,
                    current: parseInt(payload.page, 10) || 1,
                },
            };

            console.log('log', result);
            yield put({
                type: 'save',
                payload: result,
            });
        },

        *onExport({ payload, callback }, { call, put }) {
            const response = yield call(queryExpCutInList, payload);
            const rsp = response.expInterCutTasks.expFileUrl;
            if (callback) callback(rsp);
        },
        *fetchMatType({ payload, callback }, { call, put }) {
            const response = yield call(queryMatType, payload);
            const result = response.matTypeCodes;
            yield put({
                type: 'saveType',
                payload: result,
            });
        },
        *fetchMatTime({ payload, callback }, { call, put }) {
            const response = yield call(queryicMatTime, payload);
            const result = response.icPlayTimeCodes;
            yield put({
                type: 'saveTime',
                payload: result,
            });
        },
        *fetchTempType({ payload, callback }, { call, put }) {
            const response = yield call(queryTempType, payload);
            const result = response.matTemplateCodes;
            yield put({
                type: 'saveTempType',
                payload: result,
            });
        },
        *fetchCutDetail({ payload, callback }, { call, put }) {
            const response = yield call(queryImeDetail, payload);
            const result = response.matExampleDet;
            yield put({
                type: 'saveImeDetail',
                payload: result,
            });
        },

        *fetchMatUrl({ payload, callback }, { call, put }) {
            const response = yield call(updateMatUrl, payload);
            const result = response.updateMatTemplate.matExampleUrl;
            if (callback) {
                callback(result)
            }
        },
        *add({ payload, callback }, { call, put }) {
            const response = yield call(addMatExample, payload);
            if (response?.result?.code !== 1000) {
                message.error('上传素材出错，请检查');
            } else {
                if (callback) callback();
            }
        },
        *addcutintask({ payload, callback }, { call, put }) {
            const response = yield call(addCutInTask, payload);
            if (response.createInterCutTask) {
                 if (callback) callback();
            } else {
                message.error('上传出错，请检查');
            }
        },
        
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                playInfo: action.payload.playInfo,
                list: action.payload.list,
                pagination: action.payload.pagination,
            };
        },
        saveType(state, action) {
            return {
                ...state,
                matTypeCodes: action.payload,
            };
        },
        saveTime(state, action) {
            return {
                ...state,
                matTimeCodes: action.payload,
            };
        },
        saveTempType(state, action) {
            return {
                ...state,
                tempType: action.payload,
            };
        },
        saveMatUrl(state, action) {
            return {
                ...state,
                matExampleUrl: action.payload,
            };
        },
        saveImeDetail(state, action) {
            return {
                ...state,
                imeDetail: action.payload,
            };
        },

    },
};
